package com.data.modules.log.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.log.entity.WsLog;
import com.data.modules.log.service.IWsLogService;
import com.data.modules.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: WS日志
 * @Author: data-boot
 * @Date:   2022-03-07
 * @Version: V1.0
 */
@Api(tags="WS日志")
@RestController
@RequestMapping("/log/wsLog")
@Slf4j
public class WsLogController extends BootController<WsLog, IWsLogService> {
	@Autowired
	private IWsLogService wsLogService;
	
	/**
	 * 分页列表查询
	 *
	 * @param wsLog
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "WS日志-分页列表查询")
	@ApiOperation(value="WS日志-分页列表查询", notes="WS日志-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(WsLog wsLog,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WsLog> queryWrapper = QueryGenerator.initQueryWrapper(wsLog, req.getParameterMap());
		Page<WsLog> page = new Page<WsLog>(pageNo, pageSize);
		//仅查询 biz 状态的数据
		// log 数据由报表列表展示
		queryWrapper.in("device_status","biz");
		IPage<WsLog> pageList = wsLogService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param wsLog
	 * @return
	 */
	@AutoLog(value = "WS日志-添加")
	@ApiOperation(value="WS日志-添加", notes="WS日志-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WsLog wsLog) {
		wsLogService.save(wsLog);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param wsLog
	 * @return
	 */
	@AutoLog(value = "WS日志-编辑")
	@ApiOperation(value="WS日志-编辑", notes="WS日志-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WsLog wsLog) {
		wsLogService.updateById(wsLog);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "WS日志-通过id删除")
	@ApiOperation(value="WS日志-通过id删除", notes="WS日志-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		wsLogService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "WS日志-批量删除")
	@ApiOperation(value="WS日志-批量删除", notes="WS日志-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wsLogService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "WS日志-通过id查询")
	@ApiOperation(value="WS日志-通过id查询", notes="WS日志-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		WsLog wsLog = wsLogService.getById(id);
		if(wsLog==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(wsLog);
	}

	 /**
	  * 通过id查询
	  *
	  * @return
	  */
	 @AutoLog(value = "WS日志-truncateTable")
	 @ApiOperation(value="WS日志-truncateTable", notes="WS日志-truncateTable")
	 @GetMapping(value = "/truncateTable")
	 public Result<?> truncateTable() {
		 wsLogService.truncateTable();
		 return Result.OK("日志已清空");
	 }



    /**
    * 导出excel
    *
    * @param request
    * @param wsLog
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WsLog wsLog) {
        return super.exportXls(request, wsLog, WsLog.class, "WS日志");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WsLog.class);
    }

	 @RequestMapping(value = "/pubScript", method = RequestMethod.PUT)
	 public Result<?> pubScript(@RequestBody WsLog wsLog) {
		 log.info("pubScript:{}", JSON.toJSONString(wsLog));
		 try{
			 wsLogService.pubScript(wsLog);
		 }catch (Exception e){
		 	log.error(e.getMessage(),e);
			 return Result.error("发布失败"+e.getMessage());
		 }

		 return Result.OK();
	 }



 }
